﻿@model AdminEditViewModel
@using Orchard.Projections.Models;
@using Orchard.Projections.ViewModels;

@{
    Layout.Title = T("Edit Query - {0}", Model.Name).Text;
    Style.Include("admin-projections.css");
}

@using (Html.BeginFormAntiForgeryPost()) {
    @Html.ValidationSummary()

    <fieldset class="bulk-actions">
        <h2>@T("Filters")</h2>
    </fieldset>
    
    <div class="manage">
        @Html.ActionLink(T("Add a new Group").Text, "AddGroup", new { controller = "Filter", id = Model.Id }, new { @class = "button primaryAction" })
    </div>

    foreach (var group in Model.FilterGroups) {
        bool isFirstGroup = group == Model.FilterGroups.First();
        if (!isFirstGroup) {
        <h2>@T("Or")</h2>
        }
        
    <table class="items">
        <thead>
            <tr>
                <th scope="col" >@T("Description")</th>
                <th scope="col" class="actions">
                    @Html.ActionLink(T("+ Add a new Filter").Text, "Add", new { controller = "Filter", id = group.Id })
                    @if (Model.FilterGroups.Count() > 1) {
                        <text> | </text>@Html.ActionLink(T("Delete group").Text, "DeleteGroup", new { controller = "Filter", id = group.Id }, new { itemprop = "RemoveUrl UnsafeUrl" })
                    }
                </th>
            </tr>
        </thead>
    @foreach (var filter in group.Filters) {
        <tr>
            <td>@filter.DisplayText</td>
            <td>
                @Html.ActionLink(T("Edit").Text, "Edit", new { controller = "Filter", id = group.Id, category = filter.Category, type = filter.Type, filterId = filter.FilterRecordId }) |
                @Html.ActionLink(T("Delete").Text, "Delete", new { controller = "Filter", id = Model.Id, filterId = filter.FilterRecordId }, new { itemprop = "RemoveUrl UnsafeUrl" })
            </td>    
        </tr>
    }
    </table>

    }

    <fieldset class="bulk-actions">
        <h2>@T("Sort Criteria")</h2>
    </fieldset>

    <table class="items">
        <thead>
            <tr>
                <th scope="col" >@T("Description")</th>
                <th scope="col" class="actions">@Html.ActionLink(T("+ Add a new Sort Criteria").Text, "Add", new { controller = "SortCriterion", id = Model.Id })</th>
            </tr>
        </thead>
    @foreach (var sortCriterion in Model.SortCriteria) {
        <tr>
            <td>@sortCriterion.DisplayText</td>
            <td>
                @Html.ActionLink(T("Edit").Text, "Edit", new { controller = "SortCriterion", id = Model.Id, category = sortCriterion.Category, type = sortCriterion.Type, sortCriterionId = sortCriterion.SortCriterionRecordId }) |
                @Html.ActionLink(T("Delete").Text, "Delete", new { controller = "SortCriterion", id = Model.Id, sortCriterionId = sortCriterion.SortCriterionRecordId }, new { itemprop = "RemoveUrl UnsafeUrl" })
                @if (sortCriterion != Model.SortCriteria.First()) {
                    <text>|</text> @Html.ActionLink(T("Up").Text, "Move", new { controller = "SortCriterion", id = sortCriterion.SortCriterionRecordId, direction = "up", queryId = Model.Id })
                }
                @if (sortCriterion != Model.SortCriteria.Last()) {
                    <text>|</text> @Html.ActionLink(T("Down").Text, "Move", new { controller = "SortCriterion", id = sortCriterion.SortCriterionRecordId, direction = "down", queryId = Model.Id })
                }
            </td>    
        </tr>
    }
    </table>

    <fieldset class="bulk-actions">
        <h2>@T("Layouts")</h2>
    </fieldset>

    <table class="items">
        <thead>
            <tr>
                <th scope="col" >@T("Description")</th>
                <th scope="col" class="actions">@Html.ActionLink(T("+ Add a new Layout").Text, "Add", new { controller = "Layout", id = Model.Id })</th>
            </tr>
        </thead>
    @foreach (var layout in Model.Layouts) {
        <tr>
            <td>@(layout.DisplayText)</td>
            <td>
                @Html.ActionLink(T("Edit").Text, "Edit", new { controller = "Layout", id = layout.LayoutRecordId }) |
                @Html.ActionLink(T("Delete").Text, "Delete", new { controller = "Layout", id = layout.LayoutRecordId }, new { itemprop = "RemoveUrl UnsafeUrl" })
            </td>    
        </tr>
    }
    </table>

    @* Render a button at the bottom only if there are several layouts in the table *@
    if (Model.Layouts.Count() > 5) {
        <div class="manage">
        @Html.ActionLink(T("Add a new Layout").Text, "Add", new { controller = "Layout", id = Model.Id }, new { @class = "button primaryAction" })
        </div>
    }
    

        
    <fieldset>
        @Html.ActionLink(T("Preview").ToString(), "Preview", new { Model.Id }, new { @class = "button" })
        @Html.ActionLink(T("Close").ToString(), "Index", new { }, new { @class = "button" })
    </fieldset>
    
}